﻿using Infrastructure;
using Microsoft.EntityFrameworkCore;
using SnowTech.SmartTms.App.Interface;
using SnowTech.SmartTms.App.Request;
using SnowTech.SmartTms.App.Response;
using SnowTech.SmartTms.App.Vehicle.Request;
using SnowTech.SmartTms.Repository;
using SnowTech.SmartTms.Repository.Domain;
using SnowTech.SmartTms.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SnowTech.SmartTms.App.Vehicle
{
    public class VehicleApp : BaseStringApp<TmsVehicle, OpenAuthDBContext>
    {
        public VehicleApp(IUnitWork<OpenAuthDBContext> unitWork, IRepository<TmsVehicle, OpenAuthDBContext> repository, IAuth auth) : base(unitWork, repository, auth)
        {
        }

        public TableData VehicleLoad(VehicleShowReq request)
        {
            var loginContext = _auth.GetCurrentUser();
            if (loginContext == null)
            {
                throw new CommonException("登录已过期", Define.INVALID_TOKEN);
            }

            var result = new TableData();//获取数据对象
            var resources = GetDataPrivilege("u");//获取数据权限
            //权限
            if (!string.IsNullOrEmpty(request.VehicleModel))
            {
                resources = resources.Where(u => u.VehicleModel.Contains(request.VehicleModel));
            }

            if (!string.IsNullOrEmpty(request.PlateNumber))
            {
                resources = resources.Where(u => u.PlateNumber.Contains(request.PlateNumber));
            }
            if (!string.IsNullOrEmpty(request.Name))
            {
                resources = resources.Where(u => u.Name.Contains(request.Name));
            }
            if (!string.IsNullOrEmpty(request.Company))
            {
                resources = resources.Where(u => u.Company.Contains(request.Company));
            }
            //分页查询
            result.data = resources
                .Skip((request.page - 1) * request.limit)
                .Take(request.limit).ToList();
            result.count =resources.Count();
            return result;
        }
        /// <summary>
        /// 车辆添加
        /// </summary>
        /// <param name="vehicle"></param>
        public void VehicleAdd(VehicleAppReq vehicle)
        {
            var obj = vehicle.MapTo<TmsVehicle>();//用AutoMapper把输入的参数转为实体
            obj.CreateTime = DateTime.Now;//创建时间
            var user = _auth.GetCurrentUser().User;//获取当前登录用户的对象
            obj.CreateUserId = user.Id;
            obj.UpdateTime = DateTime.Now;
            obj.UpdateUserId = user.Id;
            Repository.Add(obj);
        }
        public void VehicleUpt(VehicleAppReq vehicle)
        {
            var user = _auth.GetCurrentUser().User;//获取当前用户
            UnitWork.Update<TmsVehicle>(u => u.Id == vehicle.Id, u => new TmsVehicle
            {
                VehicleModel = vehicle.VehicleModel,
                PlateNumber = vehicle.PlateNumber,
                Name = vehicle.Name,
                Company = vehicle.Company,
                Lengths = vehicle.Lengths,
                Widths = vehicle.Widths,
                Heights = vehicle.Heights,
                VehicleColor = vehicle.VehicleColor,
                AcquisitionDate = vehicle.AcquisitionDate,
                OperationNumber = vehicle.OperationNumber,
                DurationInsurance = vehicle.DurationInsurance,
                ExpirationYear = vehicle.ExpirationYear,
                Maintain = vehicle.Maintain,
                VehiclePhotos = vehicle.VehiclePhotos,
                InsuranceCardPhoto = vehicle.InsuranceCardPhoto,
                CreateTime = DateTime.Now,
                CreateUserId = user.Id,
                UpdateTime = DateTime.Now,
                UpdateUserId = user.Id
                //todo要修改的字段
            });

        }
        /// <summary>
        /// 按照Id批量删除
        /// </summary>
        /// <param name="ids"></param>
        public new void VehicleDel(string[] ids)
        {
            UnitWork.ExecuteWithTransaction(() =>
            {
                UnitWork.Delete<TmsVehicle>(u => ids.Contains(u.Id));
                UnitWork.Save();
            });
            
        }







    }
}
